﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

using MongoDB.Bson;
using MongoDB.Driver;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Xml.Linq;
using MongoDB.Driver.Builders;

namespace BattleShip_MongoDB
{
    public static class UserInformation
    {
        public static BsonValue UserID;
        public static String    UserName        = "";
        public static String    UserPass        = "";
        public static String    NumberOfWin     = "";
        public static String    NumberOfLose    = "";
        
        public static Boolean   isTest          = !true;
        public static Boolean   isFirst         = true;
        public static int       step            = -1;
        public static String    gameID          = "";
        public static Boolean   isConnecting    = false;
        public static String    UserName1       = "";
        public static String    UserName2       = "";
        public static int       MTIndex         = -1;

        public static GameRoom  gameRoom;
        public static GameForm      mainGame;


        /////////////////////////////////// *** Create Users Collection *** /////////////////////////////////////

        private static void CreateUsersCollection()
        {
            MongoServer mongoBD = MongoServer.Create();
            mongoBD.Connect();

            var db = mongoBD.GetDatabase("BattleShipDataBase");

            using (mongoBD.RequestStart(db))
            {
                var collection = db.GetCollection<BsonDocument>("Users");

                //Add new user
                BsonDocument new_User = new BsonDocument()
                    .Add("UserID", BsonValue.Create(BsonType.ObjectId))
                    .Add("UserName", "Hanh")
                    .Add("Password", "bimat")
                    .Add("Status", "0")
                    .Add("NumberOfWin", "0")
                    .Add("NumberOfLost", "0");
                collection.Insert(new_User);

                // Query data 
                var query = new QueryDocument("Hanh", "bimat");

                foreach (BsonDocument item in collection.Find(query))
                {
                    BsonElement UserName = item.GetElement("UserName");
                    BsonElement Password = item.GetElement("Password");

                    Console.WriteLine("Username: {0}, Password: {1}", UserName.Value, Password.Value);

                    // show all the data
                    foreach (BsonElement element in item.Elements)
                    {
                        Console.WriteLine("Name: {0}, Value: {1}", element.Name, element.Value);
                    }
                }
                // End Query data 

                // Query data - more feature
                query = new QueryDocument("Hanh", "bimat");
                foreach (BsonDocument item in collection.Find(query))
                {
                    string json = item.ToJson();
                    // Replace data
                    JToken token = JToken.Parse(json);
                    token.SelectToken("title").Replace("some other title");

                    Console.WriteLine("Author: {0}, Title: {1}", token.SelectToken("author"), token.SelectToken("title"));
                    Console.WriteLine();

                    XNode node = JsonConvert.DeserializeXNode(json, "documents");
                    // show xml data
                    Console.WriteLine("Node:");
                    Console.WriteLine(node);
                    Console.WriteLine();
                }
                // End Query data - more feature
            }

            mongoBD.Disconnect();
        }


        /////////////////////////////////// *** User LogOut *** /////////////////////////////////////

        public static void LogOut()
        {
            MongoServer mongoBD = MongoServer.Create();
            mongoBD.Connect();

            var db = mongoBD.GetDatabase("BattleShipDataBase");

            using (mongoBD.RequestStart(db))
            {
                var collection = db.GetCollection<BsonDocument>("GameRoom");
                var query = Query.Or(
                                Query.EQ("UserName1", UserInformation.UserName),
                                Query.EQ("UserName2", UserInformation.UserName)
                                );
                collection.Remove(query);

                collection = db.GetCollection<BsonDocument>("GameProcess");
                query = new QueryDocument("UserName", UserInformation.UserName);
                collection.Remove(query);

                if (UserInformation.gameID != null)
                {
                    collection = db.GetCollection<BsonDocument>("GameProcess");
                    query = new QueryDocument("GameID", UserInformation.gameID);
                    collection.Remove(query);
                }
            }

            mongoBD.Disconnect();
        }
    }
}
